#!/bin/bash

#
# remove different echo command behaviour on different OS
#
if test "`echo -e xxx`" = "xxx"
then
    echo="echo -e"
else
    echo=echo
fi

if [ $# = 0 ]
then
	$echo "\nusage:  runtest ds_system\n"
	$echo "ds_system can be ubuntu1404, python or \"jenkins <dir>\""
	exit 1
fi

#
# test host
#

UBUNTU1404_HOST=pcrogucki
PYTHON_HOST=controls02

HOST=`hostname`
SERV_NAME=devTest


case $1 in
ubuntu1404 )
	if [ $HOST != $UBUNTU1404_HOST ]
	then
		$echo "For ubuntu 14.04, test should run on "$UBUNTU1404_HOST
		exit 1
	fi
	DIR=../bin
	DEV1="dev/test/10"
	DEV2="dev/test/11"
	DEV3="dev/test/12"
	DEV1_ALIAS="et_alias"
	;;
#
python )
	if [ $HOST != $PYTHON_HOST ]
	then
		$echo "For python, test should run on "$PYTHON_HOST
		exit 1
	fi
	DIR=suse93
	;;
#
jenkins )
	if [ $# != 2 ]
	then
		echo "Mising base directory"
		exit -1
	fi
	BASE_DIR=$2
	for i in $NODE_LABELS
	do
		echo $i

		if [ $i = "ubuntu12.04" ]
		then
			echo "Ubuntu 12.04 OS"
			DIR="$BASE_DIR/ubuntu1204/bin"
			DEV1="test/ubuntu1204/10"
			DEV2="test/ubuntu1204/11"
			DEV3="test/ubuntu1204/12"
			DEV1_ALIAS="ubuntu1204_alias"
		fi

		if [ $i = "Windows_VC9" ]
		then
			echo "Windows VC9"
			DIR="$BASE_DIR/win32/bin"
			DEV1="test/win32_vc9/10"
			DEV2="test/win32_vc9/11"
			DEV3="test/win32_vc9/12"
			DEV1_ALIAS="win32_vc9_alias"
		fi

		if [ $i = "Windows_VC10" ]
		then
			echo "Windows VC10 (64 bits)"
			DIR="$BASE_DIR/win64/bin"
			DEV1="test/win64_vc10/10"
			DEV2="test/win64_vc10/11"
			DEV3="test/win64_vc10/12"
			DEV1_ALIAS="win64_vc10_alias"
		fi
	
		if [ $i = "centos5" ]
		then
			echo "CentOS 5 OS"
			DIR="$BASE_DIR/centos5/bin"
			DEV1="test/centos5/10"
			DEV2="test/centos5/11"
			DEV3="test/centos5/12"
			DEV1_ALIAS="centos5_alias"
		fi

		if [ $i = "redhate4" ]
		then
			echo "Red Hat 4 OS"
			DIR="$BASE_DIR/redhate4/bin"
			DEV1="test/redhate4/10"
			DEV2="test/redhate4/11"
			DEV3="test/redhate4/12"
			DEV1_ALIAS="redhate4_alias"
		fi

		if [ $i = "redhate5" ]
		then
			echo "Red Hat 5 OS"
			DIR="$BASE_DIR/redhate5/bin"
			DEV1="test/redhate5/10"
			DEV2="test/redhate5/11"
			DEV3="test/redhate5/12"
			DEV1_ALIAS="redhate5_alias"
		fi

		if [ $i = "debian6" ]
		then
			echo "Debian 6 OS"
			DIR="$BASE_DIR/debian6/bin"
			DEV1="test/debian6/10"
			DEV2="test/debian6/11"
			DEV3="test/debian6/12"
			DEV1_ALIAS="debian6_alias"
		fi

		if [ $i = "debian7_tango" ]
		then
			echo "Debian 7 OS"
			DIR="$BASE_DIR/debian7/bin"
			DEV1="test/debian7/10"
			DEV2="test/debian7/11"
			DEV3="test/debian7/12"
			DEV1_ALIAS="debian7_alias"
		fi

		if [ $i = "debian8_tango" ]
		then
			echo "Debian 8 OS"
			DIR="$BASE_DIR/debian8/bin"
			DEV1="test/debian8/10"
			DEV2="test/debian8/11"
			DEV3="test/debian8/12"
			DEV1_ALIAS="debian8_alias"
		fi

	done
	;;
#
* )
	$echo "\nunknown system"
	exit 1
	;;
esac

check_return_value () {
if [ $1 != "0" ]
then
	echo "Test Suite FAILED !!!!!!!!!!!!!!!!!!"
	echo "Leaving test suite, try to fix it"
	$DIR/StopPoll $DEV1
	exit -1
fi
}

#
# First, test device server startup sequence
#

$echo "\nTesting executing command asynchronously (polling way)"
$DIR/asyn_cmd $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting reading attribute asynchronously (polling way)"
$DIR/asyn_attr $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting reading several attributes asynchronously (polling way)"
$DIR/asyn_attr_multi $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting writing attribute asynchronously (polling way)"
$DIR/asyn_write_attr $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting writing several attributes asynchronously (polling way)"
$DIR/asyn_write_attr_multi $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting mixed asynchronous and callback modes (1)"
$DIR/asyn_cb $DEV1 $DEV2
ret=$?
check_return_value $ret
#
$echo "\nTesting mixed asynchronous and callback modes (2)"
$DIR/asyn_cb2 $DEV1 $DEV2
ret=$?
check_return_value $ret
#
$echo "\nTesting executing command asynchronously (callback way)"
$DIR/asyn_cb_cmd $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting reading attribute asynchronously (callback way)"
$DIR/asyn_attr_cb $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting writing attribute asynchronously (callback way)"
$DIR/asyn_write_cb $DEV1
ret=$?
check_return_value $ret
#
$echo "\nTesting automatic callback mode"
$DIR/auto_asyn_cmd $DEV1
ret=$?
check_return_value $ret
#
